package net.ibizsys.eclipse.studio.tree;

import org.eclipse.jface.viewers.AbstractTreeViewer;
import org.eclipse.jface.viewers.DoubleClickEvent;
import org.eclipse.jface.viewers.IDoubleClickListener;
import org.eclipse.jface.viewers.IStructuredSelection;
import org.eclipse.jface.viewers.ITreeContentProvider;
import org.eclipse.jface.viewers.TreeViewer;

/**
 * 树节点双击监听对象
 * 
 * @author <a href='http://www.weibo.com/xjtoo' target='_blank'>Enmaai</a>
 */
public class IBizTreeDoubleClickListener implements IDoubleClickListener
{
	private TreeViewer viewer;

	public IBizTreeDoubleClickListener(TreeViewer viewer)
	{
		this.viewer = viewer;
	}

	@Override
	public void doubleClick(final DoubleClickEvent event)
	{
		IStructuredSelection selection = (IStructuredSelection) event.getSelection();
		if (selection == null || selection.isEmpty())
		{
			return;
		}
		if (viewer.getContentProvider() instanceof ITreeContentProvider)
		{
			ITreeContentProvider provider = (ITreeContentProvider) viewer.getContentProvider();

			if (!provider.hasChildren(selection.getFirstElement()))
			{
				return;
			}
		}
		if (viewer.getExpandedState(selection.getFirstElement()))
		{
			viewer.collapseToLevel(selection.getFirstElement(), AbstractTreeViewer.ALL_LEVELS);
		} else
		{
			viewer.expandToLevel(selection.getFirstElement(), 1);
		}
	}
}
